package com.psq.core.common.network.interceptor

import android.os.Build
import android.text.TextUtils
import android.util.Log
import com.psq.core.common.BuildConfig
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response

/**
 * Header
 */
class HeaderInterceptor : Interceptor {

    companion object {
        private const val TAG = "HeaderInterceptor"
    }

    private val secret = "c2c8f03c1a5143ef871957e529bc99e6"

    override fun intercept(chain: Interceptor.Chain): Response {
        val original = chain.request()
        val request = original.newBuilder().apply {
            header("Model", "Android")
            header("User-Agent", System.getProperty("http.agent") ?: "unknown")
            header("Content-Type", "application/json; charset=utf-8")
            header("tokenProvider", "swtspservice")

            header("timestamp", "${System.currentTimeMillis()}")
            header("version", "${Build.VERSION.SDK_INT}")
        }.build()
        if (BuildConfig.DEBUG) {
            request.headers.forEach { Log.d(TAG, it.toString()) }
        }
        return chain.proceed(request)
    }

    private fun encrypt(request: Request): String? {

        return null
    }

    private fun isJson(str: String): Boolean {
        return if (TextUtils.isEmpty(str)) false else str.matches("^\\{.*".toRegex())
    }
}